package com.uwlaser.utils;

import com.alibaba.fastjson.JSONObject;
import com.uwlaser.webservice.client.WsdlEntity;
import com.uwlaser.webservice.client.WsdlMethodEntity;
import com.uwlaser.webservice.client.WsdlResolver;
import com.uwlaser.webservice.client.WsdlSendRequest;
import com.uwlaser.webservice.exception.WebServiceException;
import org.dom4j.DocumentException;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author QZK
 * @Description TODO
 * @Date 2022-11-23-11:24
 * @Version 1.0
 */
public class MyWebServiceUtil {


    /**
     * 发送Post请求
     * @param url 服务地址
     * @param methodName 方法名称
     * @param bodyParams 请求参数
     * @return
     */
    public static JSONObject sendPostRequestWS(String url, String methodName,String bodyParams) throws Exception{

        return MyWebServiceUtil.sendPostRequestWS(url,methodName,bodyParams,null);
    }

    /**
     * 发送Post请求
     * @param url 服务地址
     * @param methodName 方法名称
     * @param bodyParams 请求参数
     * @param headerParams 请求头参数
     * @return
     */
    public static JSONObject sendPostRequestWS(String url, String methodName, String bodyParams, String headerParams) throws Exception {
        WsdlEntity resolve = null;
        JSONObject res = null;
        try {
            //获取Wsdl实体
            resolve = WsdlResolver.resolve(url);
            //发送HTTP请求
            res = WsdlSendRequest.sendRequest(resolve, methodName, bodyParams, headerParams);
        } catch (Exception e){
            //处理异常后抛出
            throw WebServiceException.handleException(url, methodName, bodyParams, headerParams, e);
        }
        return res;
    }

    /**
     * 查询对应方法需要的JSON数据格式
     * @param url
     * @param methodName
     * @return
     */
    public static String getMethodStrJson(String url, String methodName) throws Exception {
        //打印请求JSON
        WsdlEntity resolve = WsdlResolver.resolve(url);
        WsdlMethodEntity methodEntity = resolve.getMethodEntity(methodName);
        String soapBody = methodEntity.getSoapBody();
        String soapHeader = methodEntity.getSoapHeader();
        System.out.println("/-------------------【"+methodName+"】Body--------------------/");
        System.out.println(soapBody);
        System.out.println("/-------------------【"+methodName+"】Body--------------------/");
        System.out.println("");
        System.out.println("/-------------------【"+methodName+"】Header--------------------/");
        System.out.println(soapHeader);
        System.out.println("/-------------------【"+methodName+"】Header--------------------/");
        return soapBody;
    }

    /**
     * 根据URL获取方法列表
     * @param url
     * @return
     */
    public static Set<String> getMethods(String url) throws Exception {
        //打印请求JSON
        WsdlEntity resolve = WsdlResolver.resolve(url);
        List<WsdlMethodEntity> methods = resolve.getMethods();
        return methods.stream().map(WsdlMethodEntity :: getMethodName).collect(Collectors.toSet());
    }

    /**
     * 请求示例
     * @param args
     * @throws DocumentException
     */
    public static void main(String[] args) throws Exception {


        String url = "http://localhost:8081/mesws-web/services/svoltBusinService?wsdl";
        String methodName = "t1";
        String bodyJson = null;
        String headerJson = null;

        //本地WebService测试
        /*ArrayList<TestObject> testObjects = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            TestObject testObject = new TestObject();
            testObject.setName("name");
            testObject.setAge(88888);
            testObject.setStrings(Arrays.asList("1","2","3"));

            testObject.setObj(new TestObject("小明",18,Arrays.asList("A","B","C"),null,null));

            testObject.setObjects(Arrays.asList(
                    new TestObject("小黑",20,Arrays.asList("A","B","C"),null,null),
                    new TestObject("小红",20,Arrays.asList("A","B","C"),null,null)
            ));
            testObjects.add(testObject);
        }
        HashMap<String, Object> obj = new HashMap<String, Object>() {
            {
                put("list", testObjects);
            }
        };
        bodyJson = JSONObject.toJSONString(obj);

        LoginHeader loginHeader = new LoginHeader();
        loginHeader.setEquPassword("123");
        loginHeader.setEquUserID("6666");
        loginHeader.setOperatorPassword("888888");
        loginHeader.setOperatorUserID("34453");
        headerJson = JSONObject.toJSONString( loginHeader);*/


        //腾讯QQ在线测试
        /*url = "http://ws.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl";
        methodName = "qqCheckOnline";
        bodyJson ="{\"qqCode\":\"1175332531\"}";*/

        //天气预报测试
        /*url = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";
        methodName = "getSupportCity";
        bodyJson ="{\"byProvinceName\":\"广东\"}";*/


        //IP归属地查询
        /*url = "http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl";
        methodName = "getCountryCityByIp";
        bodyJson ="{\"theIpAddress\":\"180.106.82.153\"}";*/

        //中英文翻译
        url = "http://www.webxml.com.cn/WebServices/TranslatorWebService.asmx?wsdl";
        methodName = "getEnCnTwoWayTranslator";
        bodyJson ="{\"Word\":\"翻译\"}";

        try {
            //获取方法列表
            Set<String> methods = getMethods(url);
            System.out.println(methods);

            //输出方法JSON模板
            getMethodStrJson(url, methodName);

            //发送请求
            JSONObject jsonObject = sendPostRequestWS(url, methodName, bodyJson,headerJson);
            System.out.println(jsonObject);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }


    }

}
